import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/code_value_entity.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/a_yyt/models/floor_list_entity.dart';
import 'package:ls_work/a_yyt/models/medical_elder_entity.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/model/building_model.dart';
import 'package:ls_work/ui/select_old_page.dart';
import 'package:ls_work/viewmodel/model/combo_data_model.dart';

import '../../model/customer_info_model.dart';
import '../../model/event_manage/event_manage_model.dart';

class AddEventViewModel extends BaseViewModel {
  AddEventViewModel(BuildContext context) : super(context) {
    dateTime = currentDate.toDateStr(format);
  }

  ///长者
  MedicalElderInfo? selectOld;

  var comboModel = ComboDataModel();
  var buildingModel = BuildingModel();
  var eventModel = EventManageModel();
  var customerModel = CustomerInfoModel();

  var eventTypeList = <CodeValue>[];

  ///事件类型
  CodeValue? selectedType;
  var buildingList = <FloorEntity>[];

  ///事件地点
  FloorEntity? selectedBuilding;


  var format = ['yyyy', '-', 'mm', '-', 'dd', ' ', 'HH', ':', 'mm', ':', 'ss'];
  var currentDate = DateTime.now();

  ///发生时间
  var dateTime = "";

  ///详细地址
  var locationAddr = "";

  ///事件经过
  var eventProcess = "";

  var files = <String>[];

  void selectOldInfo() {
    Go().push(SelectOldPage((old) {
      selectOld = old;
      finish();
      notifyListeners();
    }));
  }

  @override
  void onCreated() {
    super.onCreated();
  }

  Future<void> getBuilding() async {
    showLoadingDialog();
    try {
      buildingList = await buildingModel.getBuildingList(rows: 100);
      hideDialog();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }

  getEventType() async {
    showLoadingDialog();
    try {
      eventTypeList = await comboModel.getComboData("266213");
      hideDialog();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }

  Future<void> selectBuilding() async {
    if (buildingList.isEmpty) {
      await getBuilding();
    }
    DataPickerView(context, buildingList, onSubmitListener: (p1, p2, p3) {
      selectedBuilding = buildingList[p1];
      notifyListeners();
    }, currentFirstData: selectedBuilding).show();
  }

  Future<void> selectEventType() async {
    if (eventTypeList.isEmpty) {
      await getEventType();
    }
    DataPickerView(context, eventTypeList, onSubmitListener: (p1, p2, p3) {
      selectedType = eventTypeList[p1];
      notifyListeners();
    }, currentFirstData: selectedType).show();
  }

  void selectTime() {
    DatePicker.showDateTimePicker(
        context, currentTime: currentDate, onConfirm: (date) {
      currentDate = date!;
      dateTime = date.toDateStr(format);
      notifyListeners();
    });
  }

  Future<void> submitCreate() async {
    // if (selectOld == null) {
    //   showToast("请选择长者");
    //   return;
    // }
    if (selectedType == null) {
      showToast("请选择事件类型");
      return;
    }
    if (dateTime.isNullOrEmpty()) {
      showToast("请选择发生时间");
      return;
    }
    if (selectedBuilding == null) {
      showToast("请选择发生位置");
      return;
    }
    if (locationAddr.isNullOrEmpty()) {
      showToast("请输入详细位置");
      return;
    }
    if (eventProcess.isNullOrEmpty()) {
      showToast("请输入事件经过");
      return;
    }

    showLoadingDialog(msg: '提交中...');
    try{
      await eventModel.createEvent(elderId: selectOld?.id,
          eventType: selectedType?.codeValue,
          occurrenceTime: dateTime,
          occurrencePlace: selectedBuilding?.id,
          placeDetail: locationAddr,
          eventProcess: eventProcess,attatchIds: files);
      hideDialog();
      finish(data: true);
    }catch(e){
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }

  @override
  void receiveMessage(SendMessageEvent event) {
    super.receiveMessage(event);
    if(event.msgCode==MessageConfig.NFC_READ_IC_CARD){
      var cardNumber = event.obj as String;
      getCustomerByCard(cardNumber);
    }

  }

  Future<void> getCustomerByCard(String cardNumber) async {
    showLoadingDialog();
    try{
      var old = await customerModel.getCardAccount(physicalCardNo: cardNumber);
      var info = await customerModel.getElderInfo(old!.data!.human!.id.toString());
      hideDialog();
      selectOld = MedicalElderInfo.fromJson(info.toJson());
      notifyListeners();
    }catch(e){
      showToast(e.toNetError());
      hideDialog();
    }
  }
}